<?php
class App_Source_Users_Entity extends Mozg_Property_Params
{
	private $_allGroups = false;
	private $_availableGroups = false;
	private $_access = false;

    protected $_isError = false;
    protected $_errorMessage;

	public function GetUserGroup()
	{
		return $this->GetGroup()->GetId();
	}

	public function SetGroupId($groupId)
	{
		if ($groupId == $this->GetGroup()->GetId()) return $this;

		$obj = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $groupId));
		$this->SetGroup($obj);
	}

	public function SetPassword($password)
	{
		$this->Set('password', $password);

		if (strlen($password))
		{
			$this->SetPasswordHash(Mozg::Lib('passhash')->HashPassword($password));
		}
	}

    public function IsSuperUser()
    {
    	return $this->GetId() == Mozg_Cfg::Get('global/superuser')->GetId();
    }

    private function SetAllGroups()
    {
        $this->_allGroups = false;
        $this->_availableGroups = false;
        $this->_access = false;

        return $this;
    }

    public function GetAllGroups()
    {
    	if (false === $this->_allGroups)
    	{
	        $this->_allGroups = $this->GetGrantedGroups();
	        $this->_allGroups[] = $this->GetGroup()->GetId();
	        $this->_allGroups = array_unique($this->_allGroups);
    	}

        return $this->_allGroups;
    }

    /**
		возвращает список доступных юзеру групп
	*/
    public function GetAvailableGroups()
    {
    	if (false === $this->_availableGroups)
    	{
			$this->_availableGroups = array_keys(Mozg_Mapper::Get('Groups')->GetList(array('parentId' => $this->GetAllGroups(), 'levels' => 100)));
    	}

		return $this->_availableGroups;
    }

    private function LoadGroupsAccess()
    {
        if (false === $this->_access)
        {
        	$this->_access = Mozg_Mapper::Get('Groups')->GetMergedGroupAccessFromCache($this->GetAllGroups());
        }
    }

    /**
     *
     * @param $moduleId - id или имя модуля
     * @param $ctrlId - id или имя контроллера
     * @param $actionId - id или имя экшена
     * @return unknown_type
     */
    public function CheckAccess($moduleId, $ctrlId, $actionId)
    {
    	if (!$moduleId || !$ctrlId || !$actionId) return true;

        if (!is_int($moduleId) && !is_int($ctrlId) && !is_int($actionId))
        {
        	$module = Mozg_Module::GetDetails($moduleId);
        	$ctrl = Mozg_Module::GetCtrlDetails($moduleId, $ctrlId);
        	$action = Mozg_Module::GetActionDetails($moduleId, $ctrlId, $actionId);

        	$moduleId = $module['id'];
        	$ctrlId = $ctrl['id'];
        	$actionId = $action['id'];
        }

        $moduleId = (int)$moduleId;
        $ctrlId = (int)$ctrlId;
        $actionId = (int)$actionId;

        if (!$moduleId || !$ctrlId || !$actionId) return true;

        if ($this->IsSuperUser()) return true;

        $this->LoadGroupsAccess();
        if (!isset($this->_access[$moduleId])) return false;

        return Mozg_Mapper::Get('Groups')->CheckAccess($this->_access[$moduleId], $ctrlId, $actionId);
    }

    /**
		работа в сообщениями об ошибках
     */
    public function SetError ($message)
    {
        $this->_isError = true;
        $this->_errorMessage = $message;
        return $this;
    }

    public function IsError ()
    {
        return $this->_isError;
    }

    public function GetErrorMessage ()
    {
        return $this->_errorMessage;
    }

    public function GetFullName()
    {
    	$fullName = $this->Get('fullName');
    	if (!strlen($fullName))
    	{
    		$fullName = $this->GetUserName();
    	}

    	return $fullName;
    }
}